<?php 
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.controller');
JTable::addIncludePath(JPATH_COMPONENT.DS.'tables');

class SimplelistingControllerListings extends JController
{
    function __construct( $default = array() )
    {
        parent::__construct( $default );
        $this->registerTask( 'apply', 'save' );
//          $this->registerTask( 'add','edit' );
    }


    function display()
    {
        
        JRequest::setVar('view', 'listings');
        parent::display();
    }
    
    function edit()
    {
        $cids = JRequest::getVar('cid', null, 'DEFAULT', 'array' );
        if($cids === null){
                JError::raiseError(500, JText::_('CID_MISSING_MSG'));
        }
        $simplelistingId = (int)$cids[0];

        $viewName   = JRequest::getVar( 'view', 'listings' );
        $viewLayout = JRequest::getVar( 'layout', 'detail' );
        $view = & $this->getView('listings', 'html');
        $view = & $this->getView($viewName);

        $model = & $this->getModel('listings');
        if (!$model){
                JError::raiseError(500, JText::_('MODEL_MISSING_MSG'));
        }
        $view->setModel($model, true);
        $simplelisting = $model->getSimplelistingLists($simplelistingId);

        $view->setLayout($viewLayout);
        $view->displayEdit($simplelistingId);
    }
    
    function add()
    {
        $viewName   = JRequest::getVar( 'view', 'listings' );
        $viewLayout = JRequest::getVar( 'layout', 'detail' );
        $view = & $this->getView('listings', 'html');
        $view = & $this->getView($viewName);

        $model = & $this->getModel('listings');
        if (!$model){
                JError::raiseError(500, JText::_('MODEL_MISSING_MSG'));
        }
        $view->setModel($model, true);
        $simplelisting = $model->getNewSimplelisting();

        $view->setLayout($viewLayout);
        $view->displayAdd();
    }
    
    function save(){
        JRequest::checkToken() or jexit('Invalid Token');
        $simplelisting = array(
            'id'             => JRequest::getVar( 'id', '', 'POST' ),
            'name'           => JRequest::getVar( 'name', '', 'POST' ),
            'description'    => JRequest::getVar( 'description', '', 'POST', 'string', JREQUEST_ALLOWHTML),
            'address'        => JRequest::getVar( 'address', '', 'POST' ),
            'countries_id'   => JRequest::getInt( 'countries_id', 0, 'POST' ),
            'price'          => JRequest::getFloat( 'price', 0.0, 'POST' ),
            'type_id'        => JRequest::getInt( 'type_id', 0 , 'POST' ),
            'category_id'    => JRequest::getInt( 'category_id', 0 , 'POST' ),
            'extra_id'       => JRequest::getInt( 'extra_id', 0 , 'POST' ),
            'layout_id'      => JRequest::getInt( 'layout_id', 0 , 'POST' ),
            'published'      => JRequest::getInt( 'published', 0 , 'POST' )
        );
        
            if ($create_by = JRequest::getInt( 'create_by', 0 , 'POST' )){
                $simplelisting['create_by'] = $create_by;
            }
            if ($date_created = JRequest::getVar( 'date_created', '' , 'POST' )){
                $simplelisting['date_created'] = $date_created;
            }
            
            if ($modify_by = JRequest::getInt( 'modify_by', 0 , 'POST' )){
                $simplelisting['modify_by'] = $modify_by;
            }
            if ($date_modified = JRequest::getVar( 'date_modified', '' , 'POST' )){
                $simplelisting['date_modified'] = $date_modified;
            }
            
            if ($facilities = JRequest::getVar('facilities', array(0), 'POST', 'array' )){
                if ( !empty($facilities) && is_array($facilities) ) {
		    
                sort( $facilities );
                $facilities = implode(',' , $facilities );
		    
                } else {
                    
                    $facilities = '';
                }
                $simplelisting['facilities'] = $facilities;
                
            }

                $id = JRequest::getVar('id', '', 'post', 'id');
		$model = & $this->getModel('listings');
		$model->saveSimplelisting($simplelisting);


                        switch($this->getTask()) {
			case 'apply':

				$msg	= JText::_('APPLY_ITEM_MSG');
                            	$row = & JTable::getInstance('simplelisting','table');

				$link	= 'index.php?option=com_simplelisting&controller=listings&task=edit&cid[]='.$id;
				break;
			case 'save':
			default:
				$msg	= JText::_('SAVE_ITEM_MSG');
				$link	= 'index.php?option=com_simplelisting&controller=listings';
				break;
		}
		$this->setRedirect($link, $msg);
	}
    
    function publish()
    {

        $cid 	= JRequest::getVar( 'cid', array(0), 'default', 'array' );

        if($cid === null){
                JError::raiseError(500, JText::_('CID_MISSING_MSG'));
        }

        $model = $this->getModel('listings');
        $model->publish($cid, 1);

        $redirectTo = JRoute::_('index.php?option='.JRequest::getVar('option').'&controller=listings');
        $this->setRedirect($redirectTo,  JText::_('PUBLISH_ITEM_MSG'));
    }

    function unpublish()
    {

        $cid 	= JRequest::getVar( 'cid', array(0), 'default', 'array' );

        if($cid === null){
                JError::raiseError(500, JText::_('CID_MISSING_MSG'));
        }

        $model = $this->getModel('listings');
        $model->publish($cid, 0);

        $redirectTo = JRoute::_('index.php?option='.JRequest::getVar('option').'&controller=listings');
        $this->setRedirect($redirectTo, JText::_('UNPUBLISH_ITEM_MSG'));
    }

    
    function remove()
    {

        $arrayIDs = JRequest::getVar('cid', array(0), 'default', 'array' );

        if($arrayIDs === null){
                JError::raiseError(500, JText::_('CID_MISSING_MSG'));
        }

        $model = & $this->getModel('listings');
        $model->deleteSimplelisting($arrayIDs);

        $redirectTo = JRoute::_('index.php?option='.JRequest::getVar('option').'&controller=listings');
        $this->setRedirect($redirectTo, JText::_('ITEM_DELETED_MSG'));
    }
    
    function upload_main()
    {
        JRequest::checkToken() or jexit('Invalid Token');
        $file = JRequest::getVar( 'upload_file', null, 'files', 'array' );
        $id = JRequest::getVar('id', '', 'post', 'id');

        $model = $this->getModel('listings');
        $model->uploadFile($file, $id);

        $redirectTo = JRoute::_('index.php?option='.JRequest::getVar('option'));
        $this->setRedirect($redirectTo.'&controller=listings&task=edit&cid[]='.$id, 'Image Uploaded...');
    }
    
    function upload_second(){
        JRequest::checkToken() or jexit('Invalid Token');
        $file = JRequest::getVar( 'upload_file', array(), 'files', 'array' );
        $id = JRequest::getVar('id', '', 'post', 'id');
        
        if($file === null){ 
                JError::raiseError(500,  JText::_('PARAM_MISSING_MSG'));
        }

        $model = $this->getModel('listings');
        $model->uploadFiles($file, $id);

        $redirectTo = JRoute::_('index.php?option='.JRequest::getVar('option'));
        $this->setRedirect($redirectTo.'&controller=listings&task=edit&cid[]='.$id, 'Image Uploaded...');
    }
    
    function delete_main()
    {
		
        $id = JRequest::getVar('id', '', 'detail'); //Reads cid as an array

        if($id === null){
                JError::raiseError(500, JText::_('CID_MISSING_MSG'));
        }

        $model = & $this->getModel('listings');
        $model->deleteFile($id);

        $redirectTo = JRoute::_('index.php?option='.JRequest::getVar('option'));
        $this->setRedirect($redirectTo.'&controller=listings&view=listings&task=edit&cid[]='.$id, JText::_('IMAGE_DELETED_MSG'));
    }
    
    function delete_sec()
    {

        $id = JRequest::getVar('id', '', 'detail');
        $image = JRequest::getVar('image', '', 'detail');

        $model = & $this->getModel('listings');
        $model->deleteFiles($id,$image);
        $redirectTo = JRoute::_('index.php?option='.JRequest::getVar('option'));
        $this->setRedirect($redirectTo.'&controller=listings&task=edit&cid[]='.$id, JText::_('IMAGE_DELETED_MSG'));
    }  
    
    function cancel()
    {
        $redirectTo = JRoute::_('index.php?option='.JRequest::getVar('option').'&controller=listings');
        $this->setRedirect($redirectTo, JText::_('OPERATION_CANCELLED_MSG'));
    }
   
    function orderdown()
    {
        global $mainframe;

        $db = & JFactory::getDBO();

        $cid = JRequest::getVar( 'cid', array(), '', 'array');
        $ord = JRequest::getVar('order');


        if (isset( $cid[0] ))
        {
                $row = & JTable::getInstance('simplelisting','table');
                $row->load( (int) $cid[0] );
                $row->move(1);
        }
        $mainframe->redirect('index.php?option=com_simplelisting&controller=listings');
    }

    function orderup()
    {
        global $mainframe;

        $db = & JFactory::getDBO();

        $cid = JRequest::getVar( 'cid', array(), '', 'array');
        $ord = JRequest::getVar('order');


        if (isset( $cid[0] ))
        {
                $row = & JTable::getInstance('simplelisting','table');
                $row->load( (int) $cid[0] );
                $row->move(-1);
        }
        $mainframe->redirect('index.php?option=com_simplelisting&controller=listings');
    }

    function saveorder()
    {   JRequest::checkToken() or jexit('Invalid Token');
        $cid = JRequest::getVar( 'cid', array(0), '', 'array' );
	JArrayHelper::toInteger($cid, array(0));
	$order		= JRequest::getVar( 'order', array(0), 'post', 'array' );

	foreach($cid as $cids=>$c){
	$query = 'UPDATE #__simplelisting'
		. ' SET ordering = \'' . (int) $order[$cids]
		. '\' WHERE id = '. $c//$itemid[$cids-1]
		;
	$db =& JFactory::getDBO();
        $db->setQuery( $query );
        if (!$db->query()) { JError::raiseError(500, $db->getErrorMsg() ); }
        }
	$order = JRequest::getVar( 'order', array(0), 'post', 'array' );

        $link = 'index.php?option=com_simplelisting&controller=listings';
        $this->setRedirect($link, $msg);
    }
}
